home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / batch / strings2.zip / STRINGS1.DOC < prev   
Text File  |  1992-11-25  |  38KB  |  849 lines

  1. STRINGS.COM  (Version 1.0)             Copyright (c) 1992 
  2. -------------------------------------------------------------------------
  3.             First Published in PC Magazine September 10, 1991 (Utilities)
  4. -------------------------------------------------------------------------
  5. This documentation is for STRINGS Version 1.0
  6.  
  7.  
  8. STRINGS:
  9.         STRINGS enables batch files to manipulate strings, request and
  10. interpret user input, process files, return system information, and
  11. perform simple math by adding functions to the batch file language.
  12. Stores information in either the local or the master environment.
  13.  
  14.      Batch files are quick, simple--and frustratingly limited. They can't
  15. ask you questions or interpret your answers. They can't parse a filename,
  16. tell you how much space is left in your environment, or even perform
  17. simple math. Yet despite their shortcomings, a dozen times a day it's to
  18. batch files that we must turn for routine tasks.
  19.  
  20.      By executing STRINGS.COM from within your batch files, however, you
  21. can make .BAT files do things they could never accomplish on their own.
  22. STRINGS removes many of the restrictions imposed by the limited batch
  23. file vocabulary. For example, you'll be able to perform standard string
  24. manipulations, such as separating a filename from its extension, finding
  25. one string in another, determining the length of a string, and even
  26. reading and writing files.
  27.  
  28.      STRINGS can store its results in environment variables that other
  29. programs (or subsequent calls to STRINGS) can access. It provides simple
  30. functions that allow batch files to ask a user for input, determine
  31. exactly what he entered, and then pass that input to other programs.
  32. STRINGS even includes simple but effective math functions.
  33.  
  34. USING STRINGS
  35.  
  36.      To execute the program, either from within a batch file or from the
  37. DOS prompt, you enter STRINGS, followed by the desired command function
  38. and its necessary parameters. The full syntax is
  39.  
  40. STRINGS [/?] [/M] [/Pc] [environment var =] FUNCTION [str1][, str2][, str3]
  41.  
  42. Thus, for example, to return the left 6 characters of a string, you might
  43. enter
  44.  
  45.              STRINGS LEFT this is a string, 6
  46.  
  47.      To store the This i result you would simply insert the name of the
  48. destination environment variable, followed by an equal sign, before the
  49. command. Thus, to assign the results of this example in the environment
  50. variable VAR1, your command would be
  51.  
  52.        STRINGS VAR1 = LEFT This is a stirng, 6
  53.  
  54. STRINGS would find or create the environment variable VAR1 in the local
  55. environment and assign the value This i to it.
  56.  
  57.      STRINGS defaults to using the local command-shell environment to
  58. store environment variable results, because that is what batch files use
  59. when substituting strings for environment variables. However, by including
  60. the /M switch before the variable name, you can force STRINGS to use the
  61. system master environment instead of the local command processor
  62. environment. As will be discussed in fuller detail later, you will
  63. use this ability in establishing communications among different DOS
  64. sessions running under Windows.
  65.  
  66.      Another situation in which the /M switch comes in handy is when you
  67. "shell out" to DOS from within a running program. When a program shells
  68. out to DOS, a copy of COMMAND.COM and the accompanying environment are
  69. created. Any environment variable assignments STRINGS makes during this
  70. shelled session will be lost when you exit from DOS back into the original
  71. program, however. If you want to keep any STRINGS assignments made during
  72. a shelled session, use the /M switch.
  73.  
  74.      STRINGS provides two additional command line options. The /? switch
  75. simply displays a help list of the STRINGS function commands. These are
  76. shown in the table below - Figure 1.
  77.  
  78.      The second command line switch is /Pc. Since many strings contain
  79. spaces, by default, STRINGS uses commas instead of spaces to separate
  80. multiple command parameters. If any of the parameter strings themselves
  81. contain commas, however, you can use the /Pc switch to force STRINGS to
  82. employ any unused character as c to differentiate one string from another.
  83. Thus, for example, using /P$ changes the parse character to the dollar
  84. sign ($).
  85.  
  86.      After a parse character is found, STRINGS scans for the first nonspace
  87. character to begin the next parameter. This process has the effect of
  88. ignoring any leading spaces a parameter may have. To force STRINGS to
  89. respect leading spaces, therefore, STRINGS has been written to interpret
  90. consecutive parse characters as the beginning of a parameter.
  91.  
  92.      For example, if a string with leading spaces is to be written to a
  93. file, the line
  94.  
  95.            STRINGS WRITE c:\file.out, ,,  This line has 3 leading spaces
  96.  
  97. should be used. Without the consecutive commas, STRINGS would begin the
  98. next parameter with the letter T. Using the double parse characters,
  99. STRINGS will not append the commas to the line but will keep the proper
  100. number of spaces in the string before the word  This.
  101.  
  102.      One additional character requires special handling. DOS uses the
  103. equal sign (=) in assigning a variable name to a value. What happens,
  104. then, if the string parameter you want STRINGS to read into a variable
  105. itself contains an equal sign? You might, for example, want STRINGS to
  106. read the line 
  107.  
  108.               DEVICE=HIMEN.SYS
  109.  
  110. from your CONFIG.BAT file and assign this as VAR2.
  111.  
  112.      STRINGS handles this situation by changing the equal sign character
  113. within the string into the similar-looking but wider ASCII 205 (CDh)
  114. graphics character (=) before assigning the string to an environment
  115. variable. In this example, then, the string actually stored in the
  116. environment would then look like 
  117.  
  118.                                  VAR2=DEVICE=HIMEN.SYS
  119.  
  120.      When it reads an environment variable containing the graphics =
  121. character, STRINGS knows to make the reverse substitution of the normal
  122. equal sign. The only thing you need to be aware of is that a program
  123. other than STRINGS will not know about the need to convert the graphics
  124. equal sign back into a regular equal sign in environment variables stored
  125. by STRINGS.
  126.  
  127.      A final reminder for using STRINGS: DOS limits the length of a program
  128. command line to a maximum of 127 characters. Long strings stored with short
  129. names may run up against this limitation when interpreted.
  130.  
  131. COMMAND FUNCTIONS
  132.  
  133.      Several of the STRINGS command functions have been used illustratively
  134. above, but a more systematic description of the complete function set is
  135. in order. The functions can be divided into four main groups: (1) string
  136. manipulation; (2) file operations; (3) system functions; and (4) math
  137. functions.
  138.  
  139.      (1)   The string manipulation functions start with the LEFT and
  140. RIGHT functions. As shown in the first example, LEFT returns the first n
  141. characters of a string. RIGHT operates similarly: The first parameter
  142. you supply is the string, and the second parameter is the number of
  143. characters you want returned. In both cases, if the string is shorter
  144. than the number you specify, the entire string is returned. If the second
  145. parameter is 0, both functions return a null string.
  146.  
  147.      Assigning a null string to an environment variable removes the
  148. variable from the environment. For example, if the environment contained
  149. the variable PLAY=1234 and the command
  150.  
  151.                                        STRINGS PLAY = LEFT abcd, 0
  152.  
  153. was entered, STRINGS would erase the variable PLAY from the environment. Note
  154. that STRINGS makes no attempt to display null strings on the screen.
  155.  
  156.      The MID function is used to return a substring starting at any point
  157. within the original string. As with the LEFT and RIGHT commands, the first
  158. parameter you enter is the string itself. The second parameter is the
  159. numerical position of the starting character. The third parameter is the
  160. requested length of the string you want returned. If the starting character
  161. is higher than the length of the string, a null string is returned.
  162.  
  163.      Not surprisingly, the LENGTH function returns the length of a string.
  164. The only parameter required for this function is the string itself. The
  165. number returned is itself an ASCII string. For example, if a string is 12
  166. characters long, LENGTH returns the number 12 as an ASCII 49 followed by
  167. an ASCII 50.
  168.  
  169.      The FIND and FINDC functions search a string for a given string of
  170. characters. If the string is found, the number of the starting position
  171. of the string within the original string is returned. FIND searches for
  172. the string without regard to case; FINDC considers case in its search.
  173.  
  174.      The LOWER and UPPER functions set the case of the string. LOWER
  175. reduces all the characters in a string to lowercase; UPPER capitalizes
  176. a string.
  177.  
  178.      The CHAR and VAL functions in STRINGS correspond to the CHAR$ and
  179. VAL instructions in BASIC. The CHAR function returns an ASCII character
  180. for the number passed. For example, the STRINGS CHAR 65 command returns
  181. the character A, since 65 is the base 10 ASCII code for the character A.
  182.  
  183.      The VAL function is the inverse of the CHAR function, it returns the
  184. ASCII number for a character. For example, the VAL B function returns the
  185. number 66.
  186.  
  187.      (2)   The file operation group of STRINGS functions is best introduced
  188. starting with FILENAME, which parses out the filename from within the
  189. string supplied. For example, the command
  190.  
  191.                                          STRINGS FILENAME C:\UTIL\EDIT.COM
  192.  
  193. returns the string EDIT. Similarly, the FILEEXT function returns the file
  194. extension--in this case, COM. If a filename is longer than eight characters,
  195. only the first eight characters are returned; no more than three characters
  196. will be returned by FILEEXT. If the filename cannot be found within the
  197. string or if an illegal filespec is supplied, a null string is returned.
  198.  
  199.      The file READ and WRITE functions allow batch files to perform
  200. rudimentary file processing. While file processing via a batch file is
  201. obviously slow, it can be useful in some situations.
  202.  
  203.      The READ function reads a given line from a file. The parameters
  204. you must pass are the filename and the line number within the file to
  205. be read. STRINGS determines line numbers by counting the carriage returns
  206. (13h) in the file, starting with line 1. If an attempt is made to read
  207. either line 0 or a line beyond the end of the file, an error message is
  208. printed and an ERRORLEVEL code of 1 is returned.
  209.  
  210.      A typical use for the READ function is in parsing the results produced
  211. by piping a find file utility to a file. STRINGS can then be used to read
  212. the filenames (which will each occupy one line) from the file. These
  213. filenames could then be used in conjunction with other DOS commands to
  214. process each of the files individually.
  215.  
  216.      The WRITE function writes a string to the end of a file. If the file
  217. does not yet exist, it will be created. If the specified file has a
  218. read-only attribute, an error message is printed and an ERRORLEVEL code
  219. of 1 is returned. The WRITE command is functionally identical to the
  220. following batch file command
  221.  
  222.                              ECHO string >> filename
  223.  
  224. This command is included in STRINGS to complement the READ command.
  225.  
  226.      The FILESIZE and LINESIZE functions both return information about
  227. the size of a file. FILESIZE returns the size of a file in bytes;
  228. LINESIZE returns the number of lines in the specified file.
  229.  
  230.      The TRUENAME function creates a fully specified filename, complete
  231. with drive letter and path. This function corresponds to the undocumented
  232. TRUENAME function available in DOS 4.0 and later. Note that network names
  233. and aliases created from the JOIN, SUBST, and ASSIGN commands are resolved
  234. to produce the actual drive and path. If an illegal filename is passed
  235. to the function, a null string is returned.
  236.  
  237.      (3)    Five system functions are provided to assist batch files in
  238. determining the system configuration: VER, ASK, ENVFREE, ENVSIZE, and
  239. MASTERVAR.
  240.  
  241.      The VER function returns a string containing the current DOS version
  242. number. The major version number is reported in the hundreds position,
  243. and the minor version numbers are in the tens and ones places. Called
  244. under DOS 3.3, for example, VER returns the string "330".
  245.  
  246.      The ASK function allows you to query the user and store his response
  247. as an environment variable. ASK can print a specified prompt string to
  248. the screen; it then waits for the user to respond with up to one line
  249. of input. For example, given the command line
  250.  
  251.                         STRINGS NAME = ASK What is your name?
  252.  
  253.       STRINGS will print the string "What is your name?" and then wait
  254. for the user to enter the response and press Enter. The response will
  255. be stored in the environment variable NAME.
  256.  
  257.      One important application of the ASK function is in accepting piped
  258. output from another DOS command. For example, to create a string
  259. containing the current directory, the following line could be used:
  260.  
  261.                         CD | STRINGS CURRDIR = ASK
  262.  
  263. The environment variable CURRDIR would then contain the current directory.
  264.  
  265.      Since the STRINGS utility depends so heavily on the environment
  266. block, it is only sensible to include the ENVFREE function, which returns
  267. the number of bytes free in the environment. If the /M parameter is used
  268. with this command, ENVFREE returns the bytes free for the master rather
  269. than the local environment.
  270.  
  271.      The command ENVSIZE returns the total size of the environment block.
  272. This function is useful when a batch file needs a larger environment and
  273. must determine the new size of that environment. As with the ENVFREE
  274. command, ENVSIZE returns the size of the master environment if the /M
  275. switch is used.
  276.  
  277.      The MASTERVAR function returns the value of an environment variable
  278. from the master environment block. This function allows a user to read a
  279. value from the master environment block and store it in the local command
  280. processor environment block.
  281.  
  282.      As suggested earlier, one important use for the MASTERVAR function
  283. is in communicating between different DOS sessions running under Windows,
  284. DESQview, or similar programs. This is accomplished by using STRINGS with
  285. the /M switch to assign a value to a variable in the master environment.
  286. Then, when in another DOS session, STRINGS can be used to read the value
  287. of that variable by invoking the MASTERVAR function.
  288.  
  289.      (4)   To round out STRINGS' capabilities, I've included simple
  290. unsigned arithmetic functions. The ADD function, as you would expect,
  291. sums two (base 10) numbers. The numbers are passed as strings, and the
  292. result is a string containing the sum. The remaining SUB, MUL, and DIV
  293. functions perform as would be expected, taking two numbers as input and
  294. returning the result.
  295.  
  296.      If an illegal or overlarge number is passed to any of the math
  297. functions, the function is aborted, and a null string is returned. STRINGS
  298. also handles overflows, underflows, and divide by zero errors with the
  299. same null string result. Since STRINGS uses unsigned 32-bit arithmetic,
  300. however, the possible values for numbers range from 0 to a rather hefty
  301. 4,294,967,295.
  302.  
  303.      An additional function of the SUB command should be noted. SUB
  304. responds with an ERRORLEVEL code of 1 if the result is negative. This
  305. enables the function to serve as a useful compare command. For example,
  306. if one of your batch files uses the CALL command--which is available
  307. only under DOS 3.3 and later--you might want to include the following
  308. sequence: 
  309.  
  310.                  STRINGS ver = VER
  311.                  STRINGS SUB %VER%, 330
  312.                  IF ERRORLEVEL 1 GOTO end
  313.                  CALL NEWFILE.BAT
  314.                  :END
  315.  
  316. Useful examples of actual batch files that make use of STRINGS are presented
  317. and discussed in the next section.
  318.  
  319. STRINGS AT WORK
  320.  
  321.      The test of a utility such as STRINGS is not how good it sounds when
  322. described in an article but how useful it proves in your own batch files.
  323. Presented here, therefore, are some short batch files that use the commands
  324. in STRINGS to perform functions from simple looping to file processing.
  325.  
  326.       One of the simple things that batch files cannot manage is a FOR
  327. loop with more than one line. LOOP.BAT (see Figure 2 below), is a short
  328. batch file, that uses the ADD function of STRINGS to loop though a
  329. sequence nine times.
  330.  
  331.      A frequent batch file problem is to determine how a filename was
  332. entered. Did the user type just the filename or was the file extension
  333. included? There is no easy way to parse filenames with standard batch
  334. files, but with STRINGS, it's a piece of cake. PARSE.BAT (see Figure 3 below)
  335. uses STRINGS to parse the filename. This batch file examines the %1 parameter
  336. to see if the user typed the filename on the command line. If not, the ASK
  337. function of STRINGS is used to query the filename from the user.
  338.  
  339.      Even after the filename has been entered, a problem remains: Does the
  340. filename have an extension?  The FILENAME function of STRINGS is used to
  341. ensure that there is no extension. (If no default extension was placed in
  342. %2, and one wasn't typed by the user, the use is ASKed for an extension.)
  343. Then the extension (either the default or user-entered) is appended with
  344. the DOS SET command.
  345.  
  346.      Once the filename has been parsed, the DOS IF EXIST command is used
  347. to indicate whether the file actually exists. If the file does not exist,
  348. the .BAT file prints an error message, deletes the environment variable,
  349. and exits. If the file does exist, the environment variable, FNAME, now
  350. contains the correct filename.
  351.  
  352.       One problem that STRINGS users will soon encounter is the dependence
  353. of the program on available space in the environment. To solve this problem,
  354. a technique demonstrated in the example SHELL.BAT (see Figure 4 below),
  355. can be used.
  356.  
  357.      SHELL.BAT uses ENVFREE to determine whether enough space exists in
  358. the environment. The SUB command is used to compare the bytes free value
  359. returned by ENVFREE with the required space--in this case 300 bytes.
  360.  
  361.      The SUB command returns an error code of 1 if the result is negative;
  362. this feature can be used to compare two numbers. The bytes free is subtracted
  363. from the required space. If the bytes free is less than the required space,
  364. an error code of 1 is returned.
  365.  
  366.      To prevent printing an error message to the screen, the output of
  367. the SUB command is redirected to the NUL device. The IF ERRORLEVEL command
  368. is used to detect the overflow condition.
  369.  
  370.      If there is enough space, the additional shell statement is not
  371. executed. If more environment space is needed, however, the batch file
  372. determines the required environment size by using ENVSIZE to get the
  373. current environment size and adds the necessary number of bytes.
  374.  
  375.      The proper command shell is executed by using the COMSPEC environment
  376. variable, whose /E: switch sets the size of the environment of the shell.
  377. The shell statement simply executes the same batch file that was executed
  378. using the %0 command line parameter. Since the new shell will have enough
  379. environment space, the shell statement need only be executed once.
  380.  
  381.      You've probably often wished that batch files could remember the
  382. current disk and directory. The SETLOCAL and ENDLOCAL commands in OS/2
  383. provide this function, but there is no DOS equivalent. SAVEDIR.BAT (see
  384. Figure 5 below), brings this capability to DOS.
  385.  
  386.      SAVEDIR uses a combination of piping, redirection, and the STRINGS
  387. ASK commands to put the current disk and directory into the environment
  388. variable OLDDIR. SAVEDIR then changes the disk and directory.
  389.  
  390.      The LEFT command is used to copy the first two characters from the
  391. string stored in OLDDIR to the variable DRIVE. Since the way to change
  392. the default drive in DOS is by typing the drive letter, a colon, and
  393. then hitting Enter, the variable DRIVE, containing the old disk letter,
  394. is simply executed to return to the proper drive. Return to the correct
  395. directory is achieved by simply using the CD command with the OLDDIR
  396. variable as the argument.
  397.  
  398.      The final example uses STRINGS to process a file. CAPIT.BAT
  399. (see Figure 6 below), capitalizes the instructions in an assembly file
  400. without disturbing the comments. CAPIT uses a variety of STRINGS
  401. functions, including READ, WRITE, FIND, and UPPER.
  402.  
  403.      CAPIT first calls PARSE.BAT to parse the input filename correctly.
  404. The output filename is then generated by stripping off the input filename
  405. extension and appending the .OUT extension.
  406.  
  407.      The main processing loop of CAPIT first reads a line of the input
  408. file into the LINE environment variable. The line is then searched for
  409. a semicolon, which indicates the beginning of a comment. If one is found,
  410. the line is then split into two strings: one containing the characters
  411. before the semicolon, and one containing the characters after. The former
  412. are then capitalized using UPPER and the two lines are rejoined and
  413. written to the output file.
  414.  
  415.      Note that CAPIT uses the /Pc switch to change the STRINGS parse
  416. character. Assembly files contain commas and therefore commas could not
  417. be used. The character~~************ was used, since it seldom occurs in
  418. assembly language files.
  419.  
  420.        Note also that in CAPIT, double parse characters are used to
  421. indicate the start of a parameter. In ASM files, lines often begin with
  422. a series of spaces that are important for formatting. The use of the
  423. double parse character immediately before the parameter forces STRINGS
  424. to respect the leading spaces instead of ignoring them.
  425.  
  426.      Batch files will be about as long as there is a C:> prompt. As you
  427. can see, from these examples, the functions provided by STRINGS can make
  428. your batch files more powerful, easier to write, and even a little more
  429. friendly.
  430.  
  431. PROGRAMMING NOTES
  432.  
  433.      Each time STRINGS runs, it must perform three basic steps: parse
  434. the command line, execute the command, and store the result. The job of
  435. parsing the command line is complicated by the fact that the different
  436. STRINGS commands have different required parameters.
  437.  
  438.      To begin parsing, STRINGS finds the first nonspace character. If the
  439. character is a forward slash (/) the next character is assumed to be a
  440. command line switch. Since there are only two such command line switches,
  441. parsing is done by a simple compare. If a switch is found, the proper
  442. flag is set and the overall parsing continues.
  443.  
  444.      When a word is found without the switch character prefix, an
  445. interesting problem arises. Is the word a function name or is it the
  446. name of an environment variable? Since there is no way to tell this early
  447. in the routine, STRINGS initially assumes that the word is a command and
  448. copies it into the command buffer.
  449.  
  450.      The program then scans for the next nonspace character. If that
  451. character is an equal sign (=) then the first word must have been an
  452. environment variable name and not a command function. If so, STRINGS
  453. copies the variable name from the command buffer into the destination
  454. variable name buffer. The equal sign is skipped and the next word is
  455. copied into the command buffer. The CONSOLE OUT flag is cleared to
  456. indicate that the result of the command is to be assigned to an
  457. environment variable. If an equal sign is not found, then the first word
  458. is assumed to be a parameter.
  459.  
  460.      Command parameters are copied into separate 128-byte buffers.
  461. STRINGS copies a string until the proper parse character (usually the
  462. comma) is found or until the end of the command line is detected.
  463.  
  464.      If the first two characters in a parameter consist of the parse
  465. character itself, STRINGS skips these characters but starts the parameter
  466. immediately following the parse characters. This allows strings with
  467. leading spaces to be correctly processed.
  468.  
  469.      After the command line has been parsed, the command must be
  470. validated and the proper routine must be called to execute it. To
  471. validate the command, STRINGS searches through a list of ASCIIZ
  472. strings, each of which is the name of a valid command. The end of
  473. the list is indicated by consecutive zero bytes.
  474.  
  475.      The format of the command list is the same as the format of a DOS
  476. environment block. This allows the same routine that searches the command
  477. list for valid commands to be used later to search the environment block
  478. for environment variables.
  479.  
  480.      Once the command name has been found in the list, the index into the
  481. list is used as an index into a jump table. This jump table contains
  482. pointers to each of the routines that implement the commands. The
  483. pointers are then used to call the proper command procedure.
  484.  
  485.      The command procedures themselves are actually quite simple. Each
  486. procedure accesses the necessary command parameters by loading a pointer
  487. to the proper parameter buffer. The result of the command is stored in
  488. the destination data buffer. The resulting string will later either be
  489. used as the string assigned to the destination environment variable or
  490. it will be printed to the screen as indicated by the syntax of the
  491. command.
  492.  
  493.      The carry flag is used to indicate an error occurring during
  494. execution of the command. If the carry flag is set, the result is
  495. discarded and the proper error message is printed to the screen. For
  496. all errors, STRINGS terminates with an error code of 1.
  497.  
  498.      Depending on the state of the CONSOLE OUT flag (set in the command
  499. line parse routine), the result of the command is either printed to the
  500. screen or assigned to an environment variable.
  501.  
  502.      Before a result can be assigned to an environment variable, however,
  503. the environment must be found. STRINGS can search for either the local
  504. command processor environment or the master environment block.
  505.  
  506.      Until recently, a second copy of the command processor was only
  507. used when shelling out to DOS from within another program. With the
  508. popularity of programs such as Windows 3.0, secondary command processors
  509. now occur more frequently.
  510.  
  511.      When Windows creates a DOS session, a second copy of COMMAND.COM is
  512. launched. This second copy of COMMAND.COM creates its own environment,
  513. using the strings stored in the master environment. Thus, when a SET
  514. command is used or an environment variable in a batch file is needed,
  515. it is the secondary environment, not the master environment that is
  516. referenced.
  517.  
  518.      STRINGS defaults to using the secondary command processor environment.
  519. To find that environment, STRINGS uses an undocumented pointer, located
  520. at offset 16h in the program segment prefix (PSP) block. This pointer
  521. identifies the program that started STRINGS. It is not enough just to
  522. locate STRINGS' parent, however, so STRINGS uses the same pointer in
  523. the parent's PSP to identify its parent. The trace continues until
  524. STRINGS finds a PSP that is its own parent. This is the PSP of COMMAND.COM.
  525.  
  526.      Even when the COMMAND.COM PSP is found, the job is still not over.
  527. STRINGS cannot use the environment pointer (located at 2Ch) in the
  528. COMMAND.COM PSP, since that points to the environment created for it
  529. when it was first launched. Secondary copies of COMMAND.COM create a
  530. new environment block to use when running, and this is the environment
  531. needed. This new environment is located above the copy of COMMAND.COM,
  532. and STRINGS traces the memory control block chain to find it.
  533.  
  534.      While searching for the environment of the secondary command
  535. processor is accomplished by tracing the PSP blocks, finding the master
  536. environment involves tracing memory control blocks from the start of
  537. memory. Here the search is complicated by the fact that the rules for
  538. finding the master environment have changed recently.
  539.  
  540.      Before DOS 4.0, finding the master environment was easy: It was
  541. the first memory block owned by the first installed program in memory.
  542. Finding the segment of the master environment was simply a matter of
  543. tracing the memory allocation blocks to find the first segment owned
  544. by itself, for this indicated that the block contained a PSP. Once found,
  545. the trace continued to find the first block owned by that PSP.
  546.  
  547.      DOS 4.0 introduced a new wrinkle, because now, by using the INSTALL
  548. keyword in the CONFIG.SYS file, programs can be installed before
  549. COMMAND.COM. Fortunately, however, DOS 4.0 also provides a solution.
  550. Added to the memory control blocks in DOS 4.0 is the name of the program
  551. that owns the block. This allows the trace method to work, modified only
  552. to verify the name of the PSPs found.
  553.  
  554.      Once the correct environment has been found, STRINGS must still
  555. assign the result to the right variable. The environment is searched for
  556. the variable name, and if it is found, the variable string is removed by
  557. copying the remaining variables over the target variable. The new string,
  558. composed of the environment variable, its value, and the joining equal
  559. sign, is then appended to the end of the environment strings.
  560.  
  561.      STRINGS checks the size of the environment block to determine
  562. whether the new string will fit. If the block is too small, STRINGS
  563. calls DOS function 4Ah to reallocate memory for the environment block.
  564. If the reallocation call fails, STRINGS terminates with an Out of
  565. Environment Space message.
  566.  
  567. Douglas Boling is a contributing editor of PC Magazine.
  568.  
  569. ==============================================================================
  570. The table below lists the functions supported by STRINGS, together with
  571. the parameters to supply.
  572.  
  573. FUNCTION        DESCRIPTION                             PARAMETERS TO SUPPLY
  574.  
  575. LEFT            Returns left n characters               String, number of
  576.                                                         characters
  577. RIGHT           Returns right n characters              String, number of
  578.                                                         characters
  579. MID             Returns middle n characters             String, starting
  580.                                                         character, length
  581. LENGTH          Returns string length                   String
  582. FIND            Finds position of findstring            String, findstring
  583. FINDC           Case-sensitive FIND                     String, findstring
  584. LOWER           Returns string all lowercase            String
  585. UPPER           Returns string all uppercase            String
  586. CHAR            Returns ASCII value of character        Character
  587.  
  588. VAL             Returns ASCII character of number       Number
  589.  
  590. READ            Returns a line from a file              Filename, line number
  591. WRITE           Writes string to end of file            Filename, string
  592. FILESIZE        Returns file size                       Filename
  593. LINESIZE        Returns number of lines                 Filename
  594. FILENAME        Returns filename                        Filespec
  595. FILEEXIT        Returns file extension                  Filespec
  596. TRUENAME        Returns qualified filename              Filename
  597. ASK             Returns user response                   [Prompt string]
  598. VER             Returns the DOS version number
  599. ENVFREE         Returns the bytes free in the 
  600.                 environment
  601. ENVSIZE         Returns size of the environment
  602. MASTERVAR       Returns a variable from the
  603.                 master environment
  604. ADD             Returns sum of two numbers              Number,number
  605. SUB             Returns difference of two numbers       Number,number
  606. MUL             Returns product of two numbers          Number,number
  607. DIV             Returns quotient of two numbers         Number,number
  608.  
  609. Figure 1:
  610. ========================================================================
  611.  
  612. LOOP.BAT
  613. ------------------
  614. @echo off
  615. REM ===================================================================
  616. REM Loop 10 times using STRINGS to increment a loop variable.
  617. REM ===================================================================
  618.  
  619. SET count=1
  620. :LABEL1
  621. ECHO %COUNT%
  622. STRINGS count = ADD %COUNT%, 1
  623. IF NOT .%COUNT%==.10 GOTO LABEL1
  624. SET count=
  625.  
  626. Figure 2:  LOOP.BAT uses STRINGS's ADD function to create a looping
  627.            batch file.
  628. =======================================================================
  629.  
  630. PARSE.BAT
  631. -------------------
  632. @ECHO off
  633. REM ===================================================================
  634. REM
  635. REM PARSE.BAT - A batch file that parses a filename.
  636. REM
  637. REM Entry: %1 is the filename entered by the user
  638. REM  %2 is the default filename extension desired.
  639. REM
  640. REM Exit:  %FNAME% contains the parsed filename
  641. REM
  642. REM ===================================================================
  643.  
  644. SET fname=%1
  645. SET fext=%2
  646.  
  647. IF .%1==. STRINGS fname = ASK Please enter the name of the file
  648.  
  649. STRINGS fname = FILENAME %fname%
  650.  
  651. IF .%2==. STRINGS fext = ASK Please enter the extension of the file
  652.  
  653. STRINGS fname = FILENAME %FNAME%
  654.  
  655. SET fname=%FNAME%.%FEXT%
  656. IF EXIST %FNAME% GOTO END
  657.  
  658. ECHO The filename %FNAME% does not exist.
  659. SET fname=
  660. GOTO end
  661.  
  662. :END
  663. SET fext=
  664.  
  665. Figure 3:  PARSE.BAT uses the STRINGS utility to parse filenames.
  666. ========================================================================
  667.  
  668. SHELL.BAT
  669. -------------------
  670.  
  671. @ECHO off
  672. REM ===================================================================
  673. REM
  674. REM Shell out to a new copy of COMMAND.COM to increase environment size
  675. REM
  676. REM ===================================================================
  677.  
  678. REM
  679. REM Determine the bytes free in the current environment.  If there are not
  680. REM 300 bytes free, shell out to increase the size of the environment.
  681. REM
  682.  
  683. STRINGS efree = ENVFREE
  684. STRINGS SUB 300, %EFREE% > NUL
  685. IF ERRORLEVEL 1 GOTO main
  686.  
  687.  
  688. ECHO Bytes free in current environment:
  689. STRINGS ENVFREE
  690.  
  691. REM
  692. REM Determine the size of the necessary environment by adding the
  693. REM required bytes to the current environment size.
  694. REM
  695.  
  696. STRINGS esize = ENVSIZE
  697. STRINGS esize = ADD %ESIZE%, 300
  698.  
  699. REM
  700. REM Shell out using the COMSPEC variable.  The %0 parameter is the name
  701. REM of the batch file being executed.
  702. REM
  703.  
  704. %COMSPEC% /e:%ESIZE% /c %0
  705. GOTO end
  706.  
  707. REM
  708. REM This is the main body of the batch file.  Since this is only an
  709. REM example, the only work done here is to print the number of bytes free
  710. REM in the shelled environment.
  711. REM
  712.  
  713. :MAIN
  714. ECHO.
  715. ECHO Bytes free in shelled environment
  716. STRINGS ENVFREE
  717.  
  718. :END
  719. SET EFREE=
  720. SET ESIZE=
  721.  
  722. Figure 4:  SHELL.BAT can be used to increase environment space.
  723. ========================================================================
  724.  
  725. SAVDIR.BAT
  726. -------------------
  727. @ECHO off
  728. REM ===================================================================
  729. REM
  730. REM Use strings to save the current disk and directory.
  731. REM
  732. REM ===================================================================
  733.  
  734.  
  735. REM
  736. REM Pipe the current drive and directory into a env var using the ASK
  737. REM command.
  738. REM
  739.  
  740. CD | STRINGS olddir = ask > NUL
  741.  
  742. REM
  743. REM Change to the root directory of the C: drive to leave where we were.
  744. REM
  745.  
  746. C:
  747. CD \
  748. ECHO on
  749. DIR autoexec.bat
  750. @ECHO off
  751.  
  752. REM
  753. REM Use the LEFT command to separate the drive letter from the directory
  754. REM string, then change to the proper disk.
  755. REM
  756.  
  757. STRINGS drive = LEFT %OLDDIR%, 2
  758. %DRIVE%
  759.  
  760. REM
  761. REM Use the MID command to return the directory string without the drive
  762. REM letter. Change to the proper directory using the CD command.
  763. REM
  764.  
  765. CD %OLDDIR%
  766.  
  767. SET drive=
  768. SET olddir=
  769.  
  770. Figure 5:  SAVEDIR.BAT  saves the current disk and directory.
  771. ========================================================================
  772.  
  773. CAPIT.BAT
  774. -------------------
  775. @ECHO off
  776. REM ===================================================================
  777. REM
  778. REM Use strings to capitalize the opcodes in an ASM file.
  779. REM
  780. REM ===================================================================
  781.  
  782. REM
  783. REM Use the previous example file PARSE to parse the filename.
  784. REM
  785.  
  786. CALL PARSE %1 ASM
  787.  
  788. IF .%fname%==. GOTO end
  789.  
  790. STRINGS fileout = FILENAME %FNAME%
  791. SET fileout=%FILEOUT%.OUT
  792.  
  793. SET lnum=1
  794.  
  795. REM
  796. REM Read the line to the variable 'LINE'.  If no more lines are in the
  797. REM file, STRINGS will return a nonzero return code.  Pipe the output
  798. REM to the NUL driver to avoid the 'Line not found' error message.
  799. REM
  800. :LABEL1
  801.  
  802. SET line=
  803. SET part1=
  804. SET part2=
  805.  
  806. STRINGS line = READ %FNAME%, %LNUM% > NUL
  807. IF ERRORLEVEL 1 GOTO end
  808.  
  809. REM
  810. REM Find the offset of the ; character in the line.  Don't capitalize
  811. REM characters after the ; since they are part of the comment.
  812. REM
  813.  
  814. STRINGS /p~ offset = FIND ~~%LINE%~ ;
  815. IF .%OFFSET%==.0 SET offset=128
  816.  
  817. STRINGS /p~ part1 = LEFT ~~%LINE%~ %OFFSET%
  818.  
  819. STRINGS offset = ADD %OFFSET%, 1
  820.  
  821. STRINGS /p~ part2 = MID ~~%LINE%~ %OFFSET%~ 128
  822.  
  823. STRINGS /p~ part1 = UPPER ~~%PART1%
  824.  
  825. REM
  826. REM Write the line to the file.  Change the parse character to ~ since
  827. REM the line may contain a comma.  Use double parse characters ~~ to
  828. REM force STRINGS to respect any leading spaces in the parameters.
  829. REM
  830.  
  831. STRINGS /p~ WRITE %FILEOUT%~ ~~%PART1%%PART2% >NUL
  832.  
  833. STRINGS lnum = ADD %LNUM%, 1
  834.  
  835. GOTO label1
  836.  
  837. :END
  838. SET var=
  839. SET fname=
  840. SET fileout=
  841. SET lnum=
  842. SET offset=
  843. SET line=
  844. SET part1=
  845. SET part2=
  846.  
  847. Figure 6:  CAPIT.BAT improves the readability of assembly language files
  848.            by uppercasing instructions but not comments.
  849. ========================================================================